package com.example.demo.controllers;

import com.example.demo.entity.CC;
import com.example.demo.services.CCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author: Ding XiJie
 */
@RequestMapping("/cc")
@Controller
public class CCController {
    @Autowired
    private CCService ccService;

    /**
     * @Author: Ding XiJie
     * @Description: 添加存储
     */
    @RequestMapping("/save")
    public String save(@RequestBody CC cc, Model model){
        try {
            cc = ccService.save(cc);
            model.addAttribute("msg","添加成功");
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("msg","添加失败");
        }
        return "cc.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改存储
     */
    @RequestMapping("/update")
    public String update(@RequestBody CC cc, Model model){
        try {
            cc = ccService.update(cc);
            model.addAttribute("msg","修改成功");
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("msg","修改失败");
        }
        return "cc.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除存储
     */
    @RequestMapping("/delete")
    public String delete(int ccid, Model model){
        try {
            ccService.delete(ccid);
            model.addAttribute("msg","删除成功");
        }catch (Exception e){
            model.addAttribute("msg","删除失败");
            e.printStackTrace();
        }
        return "cc.html";
    }
}
